import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';

class GalleryPhotoViewPage extends StatefulWidget {
  final List<String> images;

  final int initialPageIndex;

  const GalleryPhotoViewPage({Key key, this.images, this.initialPageIndex}) : super(key: key);

  @override
  _GalleryPhotoViewPageState createState() => _GalleryPhotoViewPageState();
}

class _GalleryPhotoViewPageState extends State<GalleryPhotoViewPage> {
  PageController _controller;
  int _currentIndex;

  @override
  void initState() {
    _controller = PageController(initialPage: widget.initialPageIndex);
    _currentIndex = widget.initialPageIndex;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.black,
      child: PhotoViewGallery.builder(
        scrollPhysics: const BouncingScrollPhysics(),
        builder: (BuildContext context, int index) {
          return PhotoViewGalleryPageOptions(
            imageProvider: NetworkImage(widget.images[index]),
            heroAttributes: PhotoViewHeroAttributes(tag: "heroTag"),
          );
        },
        itemCount: widget.images.length,
//        loadingChild: Container(color: Colors.redAccent,),
        loadingBuilder: (context, progress) => Center(
          child: Container(
            width: 20.0,
            height: 20.0,
            child: CircularProgressIndicator(
              value: progress == null ? null : progress.cumulativeBytesLoaded / progress.expectedTotalBytes,
            ),
          ),
        ),
        backgroundDecoration: null,
        pageController: _controller,
        enableRotation: false,
        onPageChanged: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }
}
